﻿using UtilZ.Dotnet.Ex.Log;

namespace WinFormApp
{
    public partial class FUdpRcvSendData : Form
    {
        public FUdpRcvSendData()
        {
            InitializeComponent();
        }

        private void FUdpSendData_Load(object sender, EventArgs e)
        {
            try
            {
                checkBoxLockLog.Checked = logControl.IsLock;
                checkBoxLockLog.CheckedChanged += CheckBoxLockLog_CheckedChanged;

                UtilZ.Dotnet.Ex.ZLog.ZLoger.OutputOption = UtilZ.Dotnet.Ex.ZLog.ZLogerOutputOptions.Loger;
                var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
                if (redirectAppenderToUI != null)
                {
                    redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
                }
                else
                {
                    this.logControl.AddLog(string.Format("{0} {1}", DateTime.Now.ToString(LogConstant.DateTimeFormat), "日志配置文件不存在"), LogLevel.Warn);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            try
            {
                if (this.IsDisposed)
                {
                    return;
                }

                this.logControl.AddLog(string.Format("{0} {1}", e.Item.Time.ToString(LogConstant.DateTimeFormat), e.Item.Content), e.Item.Level);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void CheckBoxLockLog_CheckedChanged(object sender, EventArgs e)
        {
            try
            {

                logControl.IsLock = checkBoxLockLog.Checked;
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }


        private void btnClear_Click(object sender, EventArgs e)
        {
            logControl.Clear();
        }


        private void FUdpSendData_FormClosing(object sender, FormClosingEventArgs e)
        {

        }


    
    }


}
